package com.mooer.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.mooer.R;
import com.mooer.config.Const;
import com.mooer.utils.UtilTools;
import com.mooer.utils.ui.BitmapViewBinder;
import com.mooer.utils.ui.UiUtils;

public class Search extends Activity implements OnClickListener,
		OnItemClickListener {

	private ListView listView;

	private EditText edit;

	private ImageView searchButton;

	private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

	private SimpleAdapter adapter;

	private TextView area1, area2, area3, more;

	private LinearLayout header;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.search);
		listView = (ListView) findViewById(R.id.listview);
		edit = (EditText) findViewById(R.id.search_content);
		searchButton = (ImageView) findViewById(R.id.search_btn);
		header = (LinearLayout) LayoutInflater.from(this).inflate(
				R.layout.search_header, null);
		area1 = (TextView) header.findViewById(R.id.area1);
		area2 = (TextView) header.findViewById(R.id.area2);
		area3 = (TextView) header.findViewById(R.id.area3);
		more = (TextView) header.findViewById(R.id.more);

		listView.addHeaderView(header, null, false);
		adapter = new SimpleAdapter(this, list, R.layout.near_item,
				new String[] { "icon", "name" }, new int[] { R.id.icon,
						R.id.name });
		adapter.setViewBinder(new BitmapViewBinder());
		listView.setAdapter(adapter);
		area1.setOnClickListener(this);
		area2.setOnClickListener(this);
		area3.setOnClickListener(this);
		more.setOnClickListener(this);
		searchButton.setOnClickListener(this);

		initListView();
		listView.setOnItemClickListener(this);

	}

	private void initListView() {
		Resources resources = getResources();
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("icon", resources.getDrawable(R.drawable.list_food));
		map1.put("name", Const.types.get(0));
		Map<String, Object> map2 = new HashMap<String, Object>();
		map2.put("icon", resources.getDrawable(R.drawable.list_beauty));
		map2.put("name", Const.types.get(1));
		Map<String, Object> map3 = new HashMap<String, Object>();
		map3.put("icon", resources.getDrawable(R.drawable.list_marry));
		map3.put("name", Const.types.get(2));
		Map<String, Object> map4 = new HashMap<String, Object>();
		map4.put("icon", resources.getDrawable(R.drawable.list_parenting));
		map4.put("name", Const.types.get(3));
		Map<String, Object> map5 = new HashMap<String, Object>();
		map5.put("icon", resources.getDrawable(R.drawable.list_sports));
		map5.put("name", Const.types.get(4));
		Map<String, Object> map6 = new HashMap<String, Object>();
		map6.put("icon", resources.getDrawable(R.drawable.list_education));
		map6.put("name", Const.types.get(5));
		Map<String, Object> map7 = new HashMap<String, Object>();
		map7.put("icon", resources.getDrawable(R.drawable.list_play));
		map7.put("name", Const.types.get(6));
		Map<String, Object> map8 = new HashMap<String, Object>();
		map8.put("icon", resources.getDrawable(R.drawable.list_shopping));
		map8.put("name", Const.types.get(7));
		Map<String, Object> map9 = new HashMap<String, Object>();
		map9.put("icon", resources.getDrawable(R.drawable.list_hotel));
		map9.put("name", Const.types.get(8));
		Map<String, Object> map10 = new HashMap<String, Object>();
		map10.put("icon", resources.getDrawable(R.drawable.list_service));
		map10.put("name", Const.types.get(9));
		list.add(map1);
		list.add(map2);
		list.add(map3);
		list.add(map4);
		list.add(map5);
		list.add(map6);
		list.add(map7);
		list.add(map8);
		list.add(map9);
		list.add(map10);

	}

	@Override
	public void onClick(View v) {
		if (v == more) {
			Intent intent = new Intent(this, NearMore.class);
			startActivity(intent);
		} else if (v == area1) {
			Intent intent = new Intent(this, BusinessList.class);
			intent.putExtra("type1", "搜索");
			intent.putExtra("type2", area1.getText());
			intent.putExtra("categoryId", Const.XINJIEKOU);
			startActivity(intent);
		} else if (v == area2) {
			Intent intent = new Intent(this, BusinessList.class);
			intent.putExtra("type1", "搜索");
			intent.putExtra("type2", area2.getText());
			intent.putExtra("categoryId", Const.SHANXILU);
			startActivity(intent);
		} else if (v == area3) {
			Intent intent = new Intent(this, BusinessList.class);
			intent.putExtra("type1", "搜索");
			intent.putExtra("type2", area3.getText());
			intent.putExtra("categoryId", Const.ZHUJIANGLU);
			startActivity(intent);
		} else if (v == searchButton) {
			if (edit.getText().toString().length() > 0) {
				Intent intent = new Intent(this, BusinessList.class);
				intent.putExtra("type1", "搜索");
				intent.putExtra("type2", edit.getText().toString());
				intent.putExtra("content",
						UtilTools.encodeURL(edit.getText().toString()));
				startActivity(intent);
			} else {
				UiUtils.showToast(this, "请输入您要查询的关键字");
			}
		}

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
			long position) {

		System.out.println(position);
		Intent intent = new Intent();
		intent.putExtra("type1", "搜索");
		if (position == 0) {
			intent.setClass(this, BusinessList.class);
			intent.putExtra("type2", "美食");
			intent.putExtra("categoryId", Const.MEISHI);
			startActivity(intent);
		} else if (position == 1) {
			intent.setClass(this, BusinessList.class);
			intent.putExtra("type2", "丽人");
			intent.putExtra("categoryId", Const.LIREN);
			startActivity(intent);
		} else if (position == 2) {
			intent.setClass(this, BusinessList.class);
			intent.putExtra("type2", "花嫁");
			intent.putExtra("categoryId", Const.HUAJIA);
			startActivity(intent);
		} else if (position == 3) {
			intent.setClass(this, BusinessList.class);
			intent.putExtra("categoryId", Const.QINZI);
			intent.putExtra("type2", "亲子");
			startActivity(intent);
		} else if (position == 4) {
			intent.setClass(this, BusinessList.class);
			intent.putExtra("type2", "健身");
			intent.putExtra("categoryId", Const.JIANSHEN);
			startActivity(intent);
		} else if (position == 5) {
			intent.setClass(this, BusinessList.class);
			intent.putExtra("type2", "培训");
			intent.putExtra("categoryId", Const.PEIXUN);
			startActivity(intent);
		} else if (position == 6) {
			intent.setClass(this, BusinessList.class);
			intent.putExtra("type2", "悠闲娱乐");
			intent.putExtra("categoryId", Const.YOUXIANYULE);
			startActivity(intent);
		} else if (position == 7) {
			intent.setClass(this, BusinessList.class);
			intent.putExtra("type2", "购物");
			intent.putExtra("categoryId", Const.GOUWU);
			startActivity(intent);
		} else if (position == 8) {
			intent.setClass(this, BusinessList.class);
			intent.putExtra("type2", "酒店");
			intent.putExtra("categoryId", Const.JIUDIAN);
			startActivity(intent);
		} else if (position == 9) {
			intent.setClass(this, BusinessList.class);
			intent.putExtra("type2", "生活服务");
			intent.putExtra("categoryId", Const.SHENGHUOFUWU);
			startActivity(intent);
		}

	}

}
